package com.rjwh.dingdong.client.util;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import com.aishua.lib.utils.AsStrUtils;
import com.rjwh.dingdong.client.R;
import com.rjwh.dingdong.client.onkeyshare.OnekeyShare;
import com.rjwh.dingdong.client.onkeyshare.ShareContentCustomizeCallback;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.ShareSDK;

import static java.security.AccessController.getContext;

/**
 * @author Kay
 * @ClassName ShareUtil
 * @Description
 * @date 2016-11-30
 * @history 2016-11-30 author: description:
 */

public class ShareUtil {

    //显示分享
    public static void showShare(Context context, final String title, final String shareIntroduction, final String shareUrl, final String image) {
        LogUtil.d("shareApp", "image:" + image + "; title: "+ title
                + "; content: "+ shareIntroduction
                + "; url: "+ shareUrl);
        Bitmap bmp = BitmapFactory.decodeResource(context.getResources(), R.drawable.icon_logo_pic);
        final String sharePath = BitmapFileUtil.saveBitmap(bmp);

        ShareSDK.initSDK(context);
        OnekeyShare oks = new OnekeyShare();
        // 关闭sso授权
        oks.disableSSOWhenAuthorize();
        oks.setSilent(false);
        /**
         * 新浪微博分享
         */
        oks.setShareContentCustomizeCallback(new ShareContentCustomizeCallback() {

            @Override
            public void onShare(Platform platform, Platform.ShareParams paramsToShare) {

                if ("SinaWeibo".equals(platform.getName())) {
                    if (!AsStrUtils.isEmpty(title)) {
                        paramsToShare.setTitle(title);
                        paramsToShare.setUrl(null);
                    }
                    if (!AsStrUtils.isEmpty(shareIntroduction) && !AsStrUtils.isEmpty(shareUrl)) {
                        paramsToShare.setText(shareIntroduction + shareUrl);
                    }
                    if (!AsStrUtils.isEmpty(image)) {
                        paramsToShare.setImageUrl(image);
                    }else {
                        paramsToShare.setImagePath(sharePath);
                    }
                }
            }
        });

        if (!AsStrUtils.isEmpty(title)) {
            LogUtil.d("shareApp", "title: "+ title);
            // title标题，印象笔记、邮箱、信息、微信、人人网和QQ空间使用
            oks.setTitle(title);
        }
        if (!AsStrUtils.isEmpty(shareIntroduction)) {
            LogUtil.d("shareApp", "shareIntroduction: "+ shareIntroduction);
            // text是分享文本，所有平台都需要这个字段
            oks.setText(shareIntroduction);
        }
        if (!AsStrUtils.isEmpty(shareUrl)) {
            LogUtil.d("shareApp", "shareUrl: "+ shareUrl);
            // titleUrl是标题的网络链接，仅在人人网和QQ空间使用
            oks.setTitleUrl(shareUrl);
            // url仅在微信（包括好友和朋友圈）中使用
            oks.setUrl(shareUrl);
        }
        if (!AsStrUtils.isEmpty(image)) {
            LogUtil.d("shareApp", "image: "+ image);
            oks.setImageUrl(image);
        }else {
            oks.setImagePath(sharePath);
        }

        // siteUrl是分享此内容的网站地址，仅在QQ空间使用
        oks.setSiteUrl(context.getString(R.string.share_app_site_url));

        oks.setSite(context.getString(R.string.app_name));

        // 启动分享GUI
        oks.show(context);
    }
}
